﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Admin.Business.Files
{
    public static class Create
    {
        public static Adams.Admin.DataTransfer.Files.Create.PageLoad PageLoad(int id)
        {
            return DataAccess.Files.Create.PageLoad(id);
        }

        public static DataTransfer.FormOutput Save(DataTransfer.Files.Create.FormInput input)
        {
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                var Fileinfo = DataAccess.Files.Create.GetFileInfo(input.Url, input.ID.ToInt());

                if (input.ID.ToInt() > 0)
                {
                    if (!Shared.Str.IsEqual(input.Url, Fileinfo.Url))
                    {
                        if (Fileinfo.Exists)
                        {
                            output.InvalidFields.Add(new DataTransfer.InvalidField()
                            {
                                Name = "Url",
                                Code = 4
                            });
                        }
                    }
                }
                else
                {
                    if (Fileinfo.Exists)
                    {
                        output.InvalidFields.Add(new DataTransfer.InvalidField()
                        {
                            Name = "Url",
                            Code = 4
                        });
                    }
                }

                if (output.IsValid)
                {
                    DataAccess.Files.Create.Save(input);
                    output.IsSaved = true;
                }
            }

            return output;
        }
    }
}
